﻿using System;
using System.Collections.Generic;
using System.Text;

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace Tools
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Web;

    public class IOHelper
    {
        public static bool ChkFileOccupation(string sourePath)
        {
            bool flag = false;
            try
            {
                File.Move(sourePath, sourePath);
            }
            catch (Exception)
            {
                flag = true;
            }
            return flag;
        }

        public static void CopyDir(string srcPath, string aimPath)
        {
            try
            {
                if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
                {
                    aimPath = aimPath + Path.DirectorySeparatorChar.ToString();
                }
                if (!Directory.Exists(aimPath))
                {
                    Directory.CreateDirectory(aimPath);
                }
                foreach (string str in Directory.GetFileSystemEntries(srcPath))
                {
                    if (Directory.Exists(str))
                    {
                        CopyDir(str, aimPath + Path.GetFileName(str));
                    }
                    else
                    {
                        File.Copy(str, aimPath + Path.GetFileName(str), true);
                    }
                }
            }
            catch (Exception exception1)
            {
                throw new Exception(exception1.ToString());
            }
        }

        public static void CopyFile(string orignFile, string newFile)
        {
            File.Copy(orignFile, newFile, true);
        }

        public static bool CopyFolder(string source, string destination)
        {
            try
            {
                if (destination[destination.Length - 1] != Path.DirectorySeparatorChar)
                {
                    destination = destination + Path.DirectorySeparatorChar.ToString();
                }
                if (!Directory.Exists(destination))
                {
                    Directory.CreateDirectory(destination);
                }
                foreach (string str in Directory.GetFileSystemEntries(source))
                {
                    if (Directory.Exists(str))
                    {
                        CopyFolder(str, destination + Path.GetFileName(str));
                    }
                    else
                    {
                        File.Copy(str, destination + Path.GetFileName(str), true);
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static void CreateFolder(string path)
        {
            DirectoryInfo info = new DirectoryInfo(path);
            if (!info.Exists)
            {
                info.Create();
            }
        }

        public static void CreateFolder(string path, string parentPath)
        {
            DirectoryInfo info = new DirectoryInfo(parentPath);
            if (!info.Exists)
            {
                info.CreateSubdirectory(path);
            }
        }

        public static bool DeleteFile(string fileFullName)
        {
            FileInfo info = new FileInfo(fileFullName);
            if (info.Exists)
            {
                try
                {
                    info.Delete();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }

        public static void DeleteFiles(string path, string[] extenArray, int days)
        {
            if (path != "")
            {
                foreach (string str in GetAllFiles(path, extenArray))
                {
                    FileInfo info = new FileInfo(path + str);
                    if (info.Exists && (info.CreationTime < DateTime.Now.AddDays((double) -days)))
                    {
                        try
                        {
                            info.Delete();
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }

        public static void DeleteFolder(string dir)
        {
            if (Directory.Exists(dir))
            {
                foreach (string str in Directory.GetFileSystemEntries(dir))
                {
                    if (File.Exists(str))
                    {
                        File.Delete(str);
                    }
                    else
                    {
                        DeleteFolder(str);
                    }
                }
                if (Directory.Exists(dir))
                {
                    Directory.Delete(dir, true);
                }
            }
        }

        private static long[] DirInfo(DirectoryInfo d)
        {
            long[] numArray = new long[3];
            long num = 0L;
            long num2 = 0L;
            long num3 = 0L;
            FileInfo[] files = d.GetFiles();
            num3 += files.Length;
            foreach (FileInfo info in files)
            {
                num += info.Length;
            }
            DirectoryInfo[] directories = d.GetDirectories();
            num2 += directories.Length;
            foreach (DirectoryInfo info2 in directories)
            {
                num += DirInfo(info2)[0];
                num2 += DirInfo(info2)[1];
                num3 += DirInfo(info2)[2];
            }
            numArray[0] = num;
            numArray[1] = num2;
            numArray[2] = num3;
            return numArray;
        }

        public static void FileAdd(string path, string strings)
        {
            using (StreamWriter writer = File.AppendText(path))
            {
                writer.Write(strings);
                writer.Flush();
            }
        }

        public static bool FileExist(string filePath)
        {
            return File.Exists(filePath);
        }

        public static void FileMove(string orignFile, string newFile)
        {
            File.Move(orignFile, newFile);
        }

        public static List<string> GetAllFiles(string path, string[] extenArray)
        {
            List<string> list = new List<string>();
            if (Directory.Exists(path))
            {
                list.Clear();
                foreach (string text1 in Directory.GetFileSystemEntries(path))
                {
                    string fullPath = Path.GetFullPath(text1);
                    string fileName = Path.GetFileName(text1);
                    string a = Path.GetExtension(fullPath).ToLower();
                    if ((extenArray == null) || (extenArray.Length == 0))
                    {
                        list.Add(fileName);
                    }
                    else
                    {
                        foreach (string str4 in extenArray)
                        {
                            if (string.Equals(a, str4, StringComparison.CurrentCultureIgnoreCase) || string.Equals(a, "." + str4, StringComparison.CurrentCultureIgnoreCase))
                            {
                                list.Add(fileName);
                            }
                        }
                    }
                }
            }
            return list;
        }

        public static long GetDirectoryLength(string dirPath)
        {
            if (!Directory.Exists(dirPath))
            {
                return 0L;
            }
            long num = 0L;
            DirectoryInfo info = new DirectoryInfo(dirPath);
            foreach (FileInfo info2 in info.GetFiles())
            {
                num += info2.Length;
            }
            DirectoryInfo[] directories = info.GetDirectories();
            if (directories.Length != 0)
            {
                for (int i = 0; i < directories.Length; i++)
                {
                    num += GetDirectoryLength(directories[i].FullName);
                }
            }
            return num;
        }

        public static long[] GetDirInfos(string dir)
        {
            long[] numArray1 = new long[3];
            return DirInfo(new DirectoryInfo(dir));
        }

        public static string GetFileAttibe(string filePath)
        {
            string str = "";
            FileInfo info = new FileInfo(filePath);
            string[] textArray1 = new string[] { 
                str, "详细路径:", info.FullName, "<br>文件名称:", info.Name, "<br>文件长度:", info.Length.ToString(), "字节<br>创建时间", info.CreationTime.ToString(), "<br>最后访问时间:", info.LastAccessTime.ToString(), "<br>修改时间:", info.LastWriteTime.ToString(), "<br>所在目录:", info.DirectoryName, "<br>扩展名:",
                info.Extension
            };
            return string.Concat(textArray1);
        }

        public static string GetFoldAll(string path)
        {
            string rn = "";
            return ListTreeShow(new DirectoryInfo(path), 0, rn);
        }

        public static string GetFoldAll(string path, string dropName, string tplPath)
        {
            string[] textArray1 = new string[] { "<select name=\"", dropName, "\" id=\"", dropName, "\"><option value=\"\">--请选择详细模板--</option>" };
            string rn = "";
            return (string.Concat(textArray1) + ListTreeShow(new DirectoryInfo(path), 0, rn, tplPath) + "</select>");
        }

        public static bool IsCanEdit(string strExtension)
        {
            strExtension = strExtension.ToLower();
            if (strExtension.LastIndexOf(".") >= 0)
            {
                strExtension = strExtension.Substring(strExtension.LastIndexOf("."));
            }
            else
            {
                strExtension = ".txt";
            }
            string[] strArray = new string[] { ".htm", ".html", ".txt", ".js", ".css", ".xml", ".sitemap" };
            for (int i = 0; i < strArray.Length; i++)
            {
                if (strExtension.Equals(strArray[i]))
                {
                    return true;
                }
            }
            return false;
        }

        public static bool IsSafeName(string strExtension)
        {
            strExtension = strExtension.ToLower();
            if (strExtension.LastIndexOf(".") >= 0)
            {
                strExtension = strExtension.Substring(strExtension.LastIndexOf("."));
            }
            else
            {
                strExtension = ".txt";
            }
            string[] strArray = new string[] { ".htm", ".html", ".txt", ".js", ".css", ".xml", ".sitemap", ".jpg", ".gif", ".png", ".rar", ".zip" };
            for (int i = 0; i < strArray.Length; i++)
            {
                if (strExtension.Equals(strArray[i]))
                {
                    return true;
                }
            }
            return false;
        }

        public static bool IsUnsafeName(string strExtension)
        {
            strExtension = strExtension.ToLower();
            if (strExtension.LastIndexOf(".") >= 0)
            {
                strExtension = strExtension.Substring(strExtension.LastIndexOf("."));
            }
            else
            {
                strExtension = ".txt";
            }
            string[] strArray = new string[] { 
                ".", ".asp", ".aspx", ".cs", ".net", ".dll", ".config", ".ascx", ".master", ".asmx", ".asax", ".cd", ".browser", ".rpt", ".ashx", ".xsd",
                ".mdf", ".resx", ".xsd"
            };
            for (int i = 0; i < strArray.Length; i++)
            {
                if (strExtension.Equals(strArray[i]))
                {
                    return true;
                }
            }
            return false;
        }

        public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn)
        {
            foreach (DirectoryInfo info in theDir.GetDirectories())
            {
                if (nLevel == 0)
                {
                    Rn = Rn + "├";
                }
                else
                {
                    string str = "";
                    for (int i = 1; i <= nLevel; i++)
                    {
                        str = str + "│&nbsp;";
                    }
                    Rn = Rn + str + "├";
                }
                Rn = Rn + "<b>" + info.Name.ToString() + "</b><br />";
                foreach (FileInfo info2 in info.GetFiles())
                {
                    if (nLevel == 0)
                    {
                        Rn = Rn + "│&nbsp;├";
                    }
                    else
                    {
                        string str2 = "";
                        for (int i = 1; i <= nLevel; i++)
                        {
                            str2 = str2 + "│&nbsp;";
                        }
                        Rn = Rn + str2 + "│&nbsp;├";
                    }
                    Rn = Rn + info2.Name.ToString() + " <br />";
                }
                Rn = ListTreeShow(info, nLevel + 1, Rn);
            }
            return Rn;
        }

        public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn, string tplPath)
        {
            foreach (DirectoryInfo info in theDir.GetDirectories())
            {
                Rn = Rn + "<option value=\"" + info.Name.ToString() + "\"";
                if (tplPath.ToLower() == info.Name.ToString().ToLower())
                {
                    Rn = Rn + " selected ";
                }
                Rn = Rn + ">";
                if (nLevel == 0)
                {
                    Rn = Rn + "┣";
                }
                else
                {
                    string str = "";
                    for (int i = 1; i <= nLevel; i++)
                    {
                        str = str + "│&nbsp;";
                    }
                    Rn = Rn + str + "┣";
                }
                Rn = Rn + info.Name.ToString() + "</option>";
                foreach (FileInfo info2 in info.GetFiles())
                {
                    string[] textArray1 = new string[] { Rn, "<option value=\"", info.Name.ToString(), "/", info2.Name.ToString(), "\"" };
                    Rn = string.Concat(textArray1);
                    if (tplPath.ToLower() == info2.Name.ToString().ToLower())
                    {
                        Rn = Rn + " selected ";
                    }
                    Rn = Rn + ">";
                    if (nLevel == 0)
                    {
                        Rn = Rn + "│&nbsp;├";
                    }
                    else
                    {
                        string str2 = "";
                        for (int i = 1; i <= nLevel; i++)
                        {
                            str2 = str2 + "│&nbsp;";
                        }
                        Rn = Rn + str2 + "│&nbsp;├";
                    }
                    Rn = Rn + info2.Name.ToString() + "</option>";
                }
                Rn = ListTreeShow(info, nLevel + 1, Rn, tplPath);
            }
            return Rn;
        }

        public static bool MoveFolder(string oldPath, string newPath)
        {
            try
            {
                Directory.Move(oldPath, newPath);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static string ReadFile(string path)
        {
            return ReadFile(path, null);
        }

        public static string ReadFile(string path, string pageEncode)
        {
            string str = "";
            if (string.IsNullOrEmpty(pageEncode))
            {
                pageEncode = "GB2312";
            }
            try
            {
                if (!File.Exists(path))
                {
                    return str;
                }
                using (FileStream stream = new FileStream(path, FileMode.Open))
                {
                    using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(pageEncode)))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
            catch (Exception)
            {
                return "";
            }
            return str;
        }

        private static bool RenameFile(string sourePath, string newFileName)
        {
            bool flag = false;
            string str = sourePath.Remove(sourePath.LastIndexOf(@"\"));
            if (!sourePath.Equals(str + newFileName))
            {
                try
                {
                    File.Move(sourePath, str + newFileName);
                    flag = true;
                }
                catch (Exception)
                {
                }
            }
            return flag;
        }

        public static void WriteFile(string path, string content)
        {
            if (!File.Exists(path))
            {
                FileStream stream1 = File.Create(path);
                stream1.Close();
                stream1.Dispose();
            }
            StreamWriter writer1 = new StreamWriter(path, true, Encoding.UTF8);
            writer1.WriteLine(content);
            writer1.Close();
            writer1.Dispose();
        }
    }
}

